#ifndef WINVIEW_H
#define WINVIEW_H

class Handler ;

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
			      WPARAM wParam, LPARAM lParam) ;
BOOL  CALLBACK dialogProcedure(HWND hwnd, UINT msg, 
	WPARAM wParam, LPARAM lParam);


void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC) ;

void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC) ;

class View 
{
public :
	View(HINSTANCE hins, LPCTSTR name , HWND parent = NULL): hwnd(0), width(450), height(380), 
		style(WS_OVERLAPPEDWINDOW| WS_CLIPCHILDREN), posX (CW_USEDEFAULT), posY(CW_USEDEFAULT), menuHandle(NULL),
		styleEx (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE), instance(hins), title (name), classname(name), parent(parent)
	{


		// Create the application window
		WndClsEx.cbSize        = sizeof(WNDCLASSEX);
		WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
		WndClsEx.lpfnWndProc   = WndProcedure;
		WndClsEx.cbClsExtra    = 0;
		WndClsEx.cbWndExtra    = 0;
		WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION) ;//loadIcon (MAKEINTRESOURCE(IDI_APPLICATION), 32, 32);
		WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
		WndClsEx.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
		WndClsEx.lpszMenuName  = NULL;
		WndClsEx.lpszClassName = classname;
		WndClsEx.hInstance     =  instance;
		WndClsEx.hIconSm       =  LoadIcon(NULL, IDI_APPLICATION); //loadIcon (MAKEINTRESOURCE(IDI_APPLICATION), 16, 16);

		// Register the application

	}
	virtual ~ View(){}
	void setMenuName(LPCTSTR name)          { WndClsEx.lpszMenuName = name; };
	virtual int create() 
	{
		RegisterClassEx(&WndClsEx);
		hwnd = ::CreateWindowEx(styleEx,           // window border with a sunken edge
                              classname,            // name of a registered window class
                              title,                // caption of window
                              style,             // window style
                              posX,                    // x position
                              posY,                    // y position
                              width,                // witdh
                              height,               // height
                              parent,         // handle to parent window
                              NULL , //menuHandle,           // handle to menu
                              instance,             // application instance
                              (LPVOID)handler);  // window creation data
		//::ShowWindow(hwnd, SW_SHOWNORMAL);
		return 0;
	}
	virtual int show(int cmdShow=SW_SHOWDEFAULT)
	{
		    ::ShowWindow(hwnd, cmdShow);
			::UpdateWindow(hwnd);
	/*
		int dCmdShow = SW_SHOWNORMAL ;
		BOOL CanShow = ::ShowWindow(hwnd, dCmdShow);

		if( CanShow )
			return 0;
		return 1;
		*/
		return 0;
	}


	HWND getHWND () const {return this->hwnd ;}
	HWND getParentHWND () const {return this->parent; }
	int addListener (Handler * h)
	{ 
		this->handler = h ; 
		//::SetWindowLongPtr (hwnd, GWLP_USERDATA, (LONG_PTR) handler ); 
		return 0;
	}

	int setWidth (unsigned w) { width  =  w; return 0;}
	int setHeight (unsigned h) { height = h; return 0;}
	int setStyle (DWORD s) {style = s; return 0;}
	int setClassStyle (UINT style) {WndClsEx.style = style; return 0;}
	int setPos (int x, int y) {posX = x ; posY = y; return 0;}
	int setWindowStyleEx(DWORD stEx) { styleEx = stEx; return 0;}
	int setWindowStyle (DWORD st) {style = st ; return 0;}
	int setWindowPos(int x, int y, int cx, int cy)
	{
		return ::SetWindowPos(this->hwnd, 0, x, y, cx, cy, SWP_NOZORDER);
	}
	void setParent(HWND handle)             { parent = handle; };
	void setMenu(HMENU handle)              { menuHandle = handle; }
	
public:
	HINSTANCE getInstance()
	{
		return this->instance; 
	}

	HICON loadIcon (LPCTSTR lpszName, int cx, int cy)
	{
		return static_cast<HICON>(LoadImage(instance, lpszName, IMAGE_ICON, cx,cy, LR_DEFAULTSIZE));
	}
protected:
	HWND hwnd;								// handle to window
	HWND parent ;							// handle to parent
	Handler * handler ;
	HINSTANCE instance;
	unsigned width ;
	unsigned height; 
	int posX;
	int posY;
	DWORD style; 
	DWORD styleEx ;
	LPCTSTR title ;
	LPCTSTR classname ;
	WNDCLASSEX WndClsEx;
	HMENU menuHandle;                       // handle to menu


} ;

#endif